import { IColor, ITriggerEvent, ModelComponent, Vec3, log, native, physics, utils } from 'cc';
import { _decorator, BoxCollider, Collider, ColliderComponent, Component, ICollisionEvent, Node } from 'cc';
import { Ball } from './billiards/Ball';
import EventManager from '../tools/EventManager';
import { App } from '../App';
import { find } from 'cc';
import UtilTool from '../tools/UtilTool';
import { input } from 'cc';
import { Input } from 'cc';
import { EventTouch } from 'cc';
import { EventConst } from '../common/Common';
import { BoxCollider2D } from 'cc';
import { Rect } from 'cc';
import { Vec2 } from 'cc';
import { GameBaseModel } from './billiards/GameBaseModel';
import { Role } from './billiards/Role';
import { PosBall } from './billiards/PosBall';
import { SpriteFrame } from 'cc';
import { Sprite } from 'cc';
import { RigidBody } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('desk')
export class desk extends Component {

    @property(Node)
    bottomPlane : Node = null;

    @property(PosBall)
    posBallCtrl : PosBall = null // 摆球控制器

    @property(SpriteFrame)
    inRoleSp : SpriteFrame = null // 球进洞图片

    gameView : GameBaseModel = null

    animNodeList : Node[] = [] // 动画节点列表

    deskRect : Rect = null // 桌面区域

    start() {
        // this.bottomPlane.getComponent(ColliderComponent).on("onTriggerEnter", this.onBallInRole, this);
        
        let deskModel : Node = find('desk_model/model', this.node.parent)
        UtilTool.printLog("deskModel", UtilTool.getModelSize(deskModel))

        this.gameView = App.Model.gameView as GameBaseModel

        // 默认设置到场外
        this.posBallCtrl.node.active = true
        this.posBallCtrl.node.position = new Vec3(100000, this.posBallCtrl.node.position.y, 100000)
        

        let wallList = []
        this.node.children.forEach((item) => {
            if(item.name == "wall") {
                wallList.push(item)
            }
        })
        let xMax = wallList[0].worldPosition.x - wallList[0].scale.x / 2
        let xMin = wallList[5].worldPosition.x + wallList[5].scale.x / 2
        let zMin = wallList[1].worldPosition.z + wallList[1].scale.x / 2
        let zMax = wallList[2].worldPosition.z - wallList[2].scale.x / 2
        this.deskRect = new Rect(zMin, xMin, Math.abs(zMax - zMin), Math.abs(xMax - xMin))

        // setTimeout(() => {
        //     let posMin_2d = this.gameView.World3DToLocal(new Vec3(xMin, 0, zMin))
        //     let posMax_2d = this.gameView.World3DToLocal(new Vec3(xMax, 0, zMax))
        //     UtilTool.printLog("pos", posMin_2d, posMax_2d)
        // }, 1000);
        


        // 6个球进洞的动画节点
        // for(let i=0; i<6; i++) {
        //     let item = new Node()
        //     let sprite = item.addComponent(Sprite)
        //     sprite.spriteFrame = this.inRoleSp
        //     item.parent = find('Canvas')
        //     this.animNodeList.push(item)
        // }
        

        // this.outputAllRigibody()

    }

    outputAllRigibody() {

        let dataList = []

        let outOne = (node : Node) => {
            let rigidbody : ModelComponent = node.getComponent(ModelComponent)
            if(rigidbody) {

                let rotate = node.eulerAngles

                let data = {
                    name : node.name,
                    pos : node.worldPosition.toString(),
                    rotation : rotate.toString(),
                    size : UtilTool.getModelSize(node).toString(),
                }

                console.log("data", data)

                dataList.push(data)
            }
        }

        let wallList = this.node.children.filter((item) => {
            return item.name == "wall"
        })

        let roleParentList = this.node.children.filter((item) => {
            return item.name.indexOf("wall_role") != -1
        })
        
        let edgeList1 = [] 
        let edgeList2 = []
        let edgeList3 = []
        let edgeRoleList = []
        let edgeOtherList = []
        let roleList = []

        roleParentList.forEach((item) => {

            let children = item.children
            children.forEach((child) => {
                if(child.name.indexOf("role") != -1 && child.name.length == 4) {
                    roleList.push(child)
                }

                if(child.name.indexOf("edge1") != -1) {
                    edgeList1.push(child)
                }

                if(child.name.indexOf("edge2") != -1) {
                    edgeList2.push(child)
                }

                if(child.name.indexOf("edge3") != -1) {
                    edgeList3.push(child)
                }

                if(child.name.indexOf("edge_role") != -1) {
                    edgeRoleList.push(child)
                }

                if(child.name.indexOf("edge_other") != -1) {
                    edgeOtherList.push(child)
                }

            })

        })

        let plane = find('plane', this.node)
        let allList = wallList.concat(roleParentList).concat(roleList).concat(edgeList1).concat(edgeList2).concat(edgeList3).concat(edgeRoleList).concat(edgeOtherList)
        allList.push(plane)

        allList.forEach((item) => {
            outOne(item)
        })

        let text = ""
        // {{121.50, 4.63, 0.41}, {0.00, 0.00, 0.00}, {10.00, 20.00, 128.00}},
        // allList.forEach((item) => {
            // text += UtilTool.formatStr('{%s, %s, %s}', item.pos.toString())
        // })
        

        UtilTool.printLog("wallList", wallList.length, edgeList1.length, edgeList2.length, edgeList3.length, edgeRoleList.length, roleList.length)

    }

    // 播放球进洞的动画
    showRoleInAnim(roleNode) {
       
    }

}

